/**
 * 中信银行信用卡登录业务处理
 */

//验证码错误重试记录
var index=0;

/**
 * 向指定ID的文本框输入值
 *
 * @param id
 * @param text
 * @return
 */
function  set_input_text_by_id( id,  text) {
    var map = new LinkedHashMap();
    map.put("method", "set_input_text_by_id");
    map.put("id", id);
    map.put("text", text);
    return sendAndReceive(map);
}

/**
 * 验证码处理
 * @returns {boolean}
 */
function inputImg() {
    index ++;
    var  code="";
    //检验是否需要输入验证码
    var flag = luaFuncs.zx_check_img();
    if (StringUtils.equals(flag, "true")) {
        var img = get_image_by_id("randImage");
        var checkImgExisted = function (img) {
            if(img.contains("fail")){
                return false
            }
            return true;
        }
        var getErrorMsg = function () {
            if (index >3){
                return "处理数据失败，请稍后再试";
            }
            return "图形验证码输入错误，请重新输入"
        }
        assertTrueOrThrowException(img, checkImgExisted, getErrorMsg);
        var taskKey =context.get(LUA_TASK_KEY) ;
        context.put(TASK_IMG_PATH, img) ;
        context.put(TASK_IMG_REGEX,"[0-9a-zA-Z]{4}");
        //获取验证码
        code = waitImgCodeBySecondsByJs(context,120,true,3);
        logger.info("code_0="+code);
        if (StringUtils.isBlank(code) || code.length() < 4) {
            logger.info("任务" + taskKey
                + "验证码信息：打码位数不足四位，手动补足 {}", code);
            code = "1234";
        }
        //输入验证码
        set_input_text_by_id("imgvalicode", code);// 输入验证码
        Thread.sleep(1000);
    }
    return code;
}

/**
 * 判断是否全部数字
 */
function isAllNum(str) {
    if (StringUtils.isEmpty(str))
        return false;
    var regex = "^\\d+$";
    var matcher=getGroup0(regex,str);
    return matcher.matches();
}

//短信处理
function inputSms() {
    clickNextById("getsms");
    //点击下一步页面,出现错误
    var imgMsg=luaFuncs.getInnerHTML("reg_phone_error");
    //有错误信息，才会包含<i></i>，没有错误信息，由于网络原因，可能返回return is null
    logger.info("imgMsg="+imgMsg);
    if(imgMsg.contains("<i></i>")) {
        var screenShots = luaFuncs.getScreenShots();
        logger.info("点击免费获取截图路径{}", screenShots);
        var errorMsg=imgMsg;
        //清空内容
        luaFuncs.clearInnerHTML("reg_phone_error");
        throwBankExceptionByErrorMsg(errorMsg.replace("<i></i>", ""));
    }
    var sms = waitSmsCodeBySeconds(context,120);
    //输入短信验证码
    var status=set_input_text_by_id("valicode", sms);
    logger.info("valicode_status="+status);
    Thread.sleep(800);
    //点击
    clickNextById("checkcode");
    Thread.sleep(200);
}

//点击下一步
function clickNextById(id) {
    var map = new LinkedHashMap();
    map.put("method", "clickNextById");
    map.put("id",id);
    return sendAndReceive(map);
}


//输入密码
function inputPassWordById(pWord){
    var map = new LinkedHashMap();
    map.put("method", "inputPassWordById");
    map.put("pWord",pWord);
    return sendAndReceive(map);
}

//点击登录
function clickLoginButton(id){
    var map = new LinkedHashMap();
    map.put("method", "clickLoginButton");
    map.put("id",id);
    return sendAndReceive(map);
}

var luaFuncs = {
    getCookie :function () {
        var map = new HashMap();
        map.put("method","getCookie");
        return sendAndReceive(map)
    },

    zx_check_sms :function(){
        var map = new HashMap();
        map.put("method","zx_check_sms");
        return sendAndReceive(map)
    },
    zx_login: function(){
        var map = new HashMap();
        map.put("method","zx_login");
        return sendAndReceive(map)
    },
    get_html_2:function(){
        var map = new HashMap();
        map.put("method","get_html_2");
        return sendAndReceive(map)
    },
    zx_receive_message:function(verifyMessage){
        var map = new HashMap();
        map.put("method","zx_receive_message");
        map.put("smsMessage",verifyMessage);
        return sendAndReceive(map)
    } ,
    zx_check_img:function(){
        var map = new HashMap();
        map.put("method","zx_check_img");
        return sendAndReceive(map)
    },

    zx_send_img:function(code){
        var map = new HashMap();
        map.put("method","zx_send_img");
        map.put("img",code);
        return sendAndReceive(map)
    },
    zx_jumpByUrl:function(url){
        var map = new HashMap();
        map.put("method","zx_jumpByUrl");
        return sendAndReceive(map)
    },
    zx_get_html:function(){
        var map = new HashMap();
        map.put("method","zx_get_html");
        return sendAndReceive(map)
    },

    zx_get_html:function() {
        var map = new LinkedHashMap();
        map.put("method", "zx_get_html");
        return sendAndReceive(map);
    },
    /**
     * 老版-登录 使用keypress、mouseleftclick等方法点击之后输入
     * @param loginTemp
     * @param password
     * @return
     * @author lijian
     */
    zx_input_usernameAndPassword:function (loginTemp, password) {
        var map = new LinkedHashMap();
        map.put("method", "zx_input_usernameAndPassword");
        map.put("loginTemp", loginTemp);
        map.put("password", password);
        return sendAndReceive(map);
    },
    /**
     * 老版本 使用keypress输入验证码  如果为第二次输入，则需要注入js将已经输入的验证码取消掉
     * @param imgCode
     * @return
     */
    zx_input_imgCode:function (imgCode,time) {
        var map = new LinkedHashMap();
        map.put("method", "zx_input_imgCode");
        map.put("imgCode", imgCode);
        map.put("times", time);
        return  sendAndReceive(map);
    },
    /**
     * 老版本-登录之后检查登录是否成功的状态
     * @return
     */
    zx_check_loginStatus:function () {
        var map = new LinkedHashMap ();
        map.put("method", "zx_check_loginStatus");
        return  sendAndReceive(map);
    },

    /**
     * 老版本  -设置短信验证码的值，并且点击登录按钮
     * @return  短信错误-->会有弹出窗口，将弹出窗口的信息完整返回
     * 			短信正确-->lua通过url的变化来确定页面跳转成功 标志为SUCCESS
     */
    zx_input_sms_and_check :function(sms) {
        var map = new LinkedHashMap ();
        map.put("method", "zx_input_sms_and_check");
        map.put("sms", sms);
        return  sendAndReceive(map);
    },

    /**
     * 截取当前屏幕的状态
     * @return
     */
    getScreenShots :function() {
        var map=new LinkedHashMap();
        map.put("method", "getScreenShots");
        return sendAndReceive(map);
    },
    /**
     * 获取文本内容
     */
    getInnerHTML:function(id){
        var map=new LinkedHashMap();
        map.put("method", "getInnerHTML");
        map.put("id",id);
        return sendAndReceive(map);
    },

    /**
     * 清空文本内容
     */
    clearInnerHTML:function(id){
        var map=new LinkedHashMap();
        map.put("method", "clearInnerHTML");
        map.put("id",id);
        return sendAndReceive(map);
    }
}

